1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
System;
5 using
System.IO;
6 using
System.Runtime.Serialization.Formatters.Binary;
7
8 public
class GameController : MonoBehaviour {
9     
public static GameController instance;
10
11     
public bool isGameStartedFirstTime;
12     
public bool isMusicOn;
13
14     
public int coins;
15     
public int weaponLevel;
16     
private GameData data;
17
18     
void Awake(){
19         CreateInstance ();
20         InitializeGameVariables ();
21     }
22
23     
// Use this for initialization
24     
void Start () {
25         
26     }
27     
28     
// Update is called once per frame
29     
void Update () {
30         
31     }
32
33     
void CreateInstance(){
34         
if (instance != null) {
35             Destroy (gameObject);
36         }
else {
37             instance =
this;
38             DontDestroyOnLoad (gameObject);
39         }
40     }
41
42     
void InitializeGameVariables(){
43         Load ();
44
45         
if (data != null) {
46             isGameStartedFirstTime = data.GetIsGameStartedFirstTime ();
47         }
else {
48             isGameStartedFirstTime =
true;
49         }
50
51         
if (isGameStartedFirstTime) {
52             isGameStartedFirstTime =
false;
53             isMusicOn =
true;
54             coins =
0;
55             weaponLevel =
1;
56             data =
new GameData ();
57             data.SetIsGameStartedFirstTime (isGameStartedFirstTime);
58             data.SetIsMusicOn (isMusicOn);
59             data.SetCoins (coins);
60             data.SetWeaponLevel (weaponLevel);
61             Save ();
62
63             Load ();
64         }
else {
65             isGameStartedFirstTime = data.GetIsGameStartedFirstTime ();
66             isMusicOn = data.GetIsMusicOn ();
67             coins = data.GetCoins ();
68             weaponLevel = data.GetWeaponLevel ();
69         }
70     }
71
72
73     
public void Save(){
74         FileStream file =
null;
75
76         
try{
77             BinaryFormatter bf =
new BinaryFormatter();
78             file = File.Create(Application.persistentDataPath +
"/data.dat");
79
80             
if(file != null){
81                 data.SetIsGameStartedFirstTime(isGameStartedFirstTime);
82                 data.SetIsMusicOn(isMusicOn);
83                 data.SetCoins (coins);
84                 data.SetWeaponLevel (weaponLevel);
85                 bf.Serialize(file, data);
86             }
87
88         }
catch(Exception e){
89             Debug.LogException (e,
this);
90         }
finally{
91             
if(file != null){
92                 file.Close ();
93             }
94         }
95     }
96
97     
public void Load(){
98         FileStream file =
null;
99
100         
try{
101             BinaryFormatter bf =
new BinaryFormatter();
102             file = File.Open(Application.persistentDataPath +
"/data.dat", FileMode.Open);
103             data = bf.Deserialize(file)
as GameData;
104         }
catch(Exception e){
105             Debug.LogException (e,
this);
106         }
finally{
107             
if(file != null){
108                 file.Close ();
109             }
110         }
111     }
112 }

113
114 [Serializable]

115 class
GameData{
116     
private bool isGameStartedFirstTime;
117     
private bool isMusicOn;
118
119     
private int coins;
120     
private int weaponLevel;
121
122     
public void SetIsGameStartedFirstTime(bool isGameStartedFirstTime){
123         
this.isGameStartedFirstTime = isGameStartedFirstTime;
124     }
125
126     
public bool GetIsGameStartedFirstTime(){
127         
return this.isGameStartedFirstTime;
128     }
129
130     
public void SetIsMusicOn(bool isMusicOn){
131         
this.isMusicOn = isMusicOn;
132     }
133
134     
public bool GetIsMusicOn(){
135         
return this.isMusicOn;
136     }
137
138     
public void SetCoins(int coins){
139         
this.coins = coins;
140     }
141
142     
public int GetCoins(){
143         
return this.coins;
144     }
145
146     
public void SetWeaponLevel(int weaponLevel){
147         
this.weaponLevel = weaponLevel;
148     }
149
150     
public int GetWeaponLevel(){
151         
return this.weaponLevel;
152     }
153
154 }


Use this for initialization

Update is called once per frame



Gõ tìm kiếm nhanh...